package com.atguigu.gmall.web.controller;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.product.client.ProductFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

/**
 * title:
 * author: Gyh
 * date:2023-04-25 19:18
 * Description:
 */
@Controller
public class IndexController {

    @Autowired
    private ProductFeignClient productFeignClient;

    @Autowired
    private TemplateEngine templateEngine;

    @GetMapping({"", "/", "/index.html"})
    public String indexHtml(Model model) {
        //1.远程调用商品微服务获取首页分类数据
        Result result = productFeignClient.getBaseCategoryList();
        //todo 首页其他数据:广告轮播图;推荐商品;热门;最新商品等都应该调用对应服务来获取 如果有这么多接口 采用CompletableFuture并行调用
        model.addAttribute("list", result.getData());
        return "/index/index";
    }

//    @GetMapping("createIndex")
//    @ResponseBody
//    public Result createIndex() throws IOException {
//        //获取后台存储的数据
//        Result result = productFeignClient.getBaseCategoryList();
//        //设置模板显示内容 E:\feiqiu\feiq\Recv Files\9.XM\nginx-1.12.2\html
//        //参数1：模板文件位置
//        //参数2：上下文对象，封装模板需要的参数
//        //参数3：写文件对象
//        Context context = new Context();
//        context.setVariable("list",result.getData());
//        FileWriter fileWriter = new FileWriter("E:\\feiqiu\\feiq\\Recv Files\\9.XM\\nginx-1.12.2\\html\\index.html");
//        templateEngine.process("/index/index",context,fileWriter);
//        return Result.ok();
//    }
}
